package cn.elwy.rcp.kit.yssuco.copy;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import cn.elwy.common.log.Logger;
import cn.elwy.common.log.LoggerFactory;
import cn.elwy.common.util.AssertUtil;
import cn.elwy.common.util.JsonUtil;
import cn.elwy.common.util.PathUtil;
import cn.elwy.common.util.VersionUtil;
import cn.elwy.common.util.io.FileUtil;

public class DeployYssUco implements Callable<Object> {
	private static Logger logger = LoggerFactory.getLogger(DeployYssUco.class);
	private String filePath;
	UcoLog ucoLog;

	public static void main(String[] args) throws IOException {
		String filePath = "D:/YSSUCO_V1.20.4.2.json";// PathUtil.getResourcePath(DeployYssUco.class,
		// "YSSUCO.json");

		DeployYssUco deployYssUco = new DeployYssUco(new UcoLog() {

			@Override
			public void print(String text) {
				logger.info(text);
			}
		});
		deployYssUco.handle(filePath);

	}

	/**
	 * 
	 */
	public DeployYssUco(UcoLog ucoLog) {
		this.ucoLog = ucoLog;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.concurrent.Callable#call()
	 */
	@Override
	public Object call() throws Exception {
		return null;
	}

	public void handle(String filePath) throws IOException {
		String json = FileUtil.readFileToString(filePath);
		List<YssUcoConfig> yucList = JsonUtil.toList(json, YssUcoConfig.class);
		for (YssUcoConfig yssUcoConfig : yucList) {
			if (yssUcoConfig.getEnable() != null
					&& yssUcoConfig.getEnable() == false) {
				continue;
			}
			String srcPath = yssUcoConfig.getSrcPath();
			List<String> deletePathList = new ArrayList<String>();
			List<String> destPathList = new ArrayList<String>();

			List<String> deletePaths = yssUcoConfig.getDeletePaths();
			List<String> destPaths = yssUcoConfig.getDestPaths();
			String rootPath = yssUcoConfig.getRootPath();
			if (AssertUtil.isNotEmpty(rootPath)) {
				srcPath = PathUtil.getAbsolutePath(rootPath, srcPath);
				if (AssertUtil.isNotEmpty(deletePaths)) {
					for (String deletePath : deletePaths) {
						deletePathList.add(PathUtil.getAbsolutePath(rootPath,
								deletePath));
					}
				}
				if (AssertUtil.isNotEmpty(destPaths)) {
					for (String destPath : destPaths) {
						destPathList.add(PathUtil.getAbsolutePath(rootPath,
								destPath));
					}
				}
			} else {
				if (AssertUtil.isNotEmpty(deletePaths)) {
					destPathList.addAll(deletePaths);
				}
				if (AssertUtil.isNotEmpty(destPaths)) {
					destPathList.addAll(destPaths);
				}
			}

			for (String deletePath : deletePathList) {
				FileUtil.deleteFile(deletePath, true);
				println("删除：" + deletePath + "文件！");
			}
			File file = new File(srcPath);
			copyJar(file, destPathList, yssUcoConfig.getDeleteFinishJar());
		}
	}

	/**
	 * @param msg
	 */
	private void println(String msg) {
		if (ucoLog != null) {
			ucoLog.print(msg);
		} else {
			logger.info(msg);
		}
	}

	protected void copyJar(File srcFile, List<String> pahts,
			Boolean deleteFinishJar) throws IOException {
		File[] listFiles = srcFile.listFiles();
		if (listFiles == null) {
			return;
		}
		for (File file2 : listFiles) {
			String name = file2.getName();
			boolean isCopy = false;
			for (String path : pahts) {
				File[] listFiles2 = new File(path).listFiles();
				if (listFiles2 != null) {
					for (File file3 : listFiles2) {
						isCopy = copyFile(name, file2, file3);
						if (isCopy) {
							break;
						}
					}
				}
			}
			if (deleteFinishJar != null && deleteFinishJar) {
				FileUtil.deleteFile(file2);
				println("删除：" + file2.getAbsolutePath() + "文件！");
			}
		}
	}

	protected boolean copyFile(String name, File srcFile, File destFile)
			throws IOException {
		String destName = destFile.getName();
		if (name.equalsIgnoreCase(destName)) {
			return copyFile(srcFile, destFile);
		}
		String fileName = VersionUtil.getFileName(name);
		String destFileName = VersionUtil.getFileName(destName);
		if (fileName.equalsIgnoreCase(destFileName)) {
			return copyFile(srcFile, destFile);
		}
		return false;
	}

	protected boolean copyFile(File srcFile, File destFile) throws IOException {
		println("复制" + srcFile.getName() + "文件到：" + destFile.getAbsolutePath());
		return FileUtil.copyFile(srcFile, destFile, true);
	}

	/**
	 * @return the filePath
	 */
	public String getFilePath() {
		return filePath;
	}

	/**
	 * @param filePath
	 *            the filePath to set
	 */
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

}
